<template>
  <div class="map-container">
    <!-- 目录树 -->
    <MapCatalog v-if="user.role" />
    <!-- 工具箱 -->
    <MapTools />
    <!-- 切底图 -->
    <div class="change-base-map">
      <img
        src="/src/assets/images/vec.png"
        :class="currentBaseMap == 'vec' ? 'currentBaseMap' : ''"
        @click="changeBaseMap('vec')"
      />
      <img
        src="/src/assets/images/img.png"
        :class="currentBaseMap == 'img' ? 'currentBaseMap' : ''"
        @click="changeBaseMap('img')"
      />
    </div>
    <!-- 地图 -->
    <div id="map">
      <!-- 鼠标位置控件 -->
      <div id="mouse-position"></div>
      <!-- 弹窗 -->
      <div id="popup-monitor" class="ol-popup"></div>
      <div id="popup" class="ol-popup"></div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { initMap } from './hooks/initMap' // 初始化地图
import { useUserStore } from '@/stores/user' // 使用pinia
import MapCatalog from './mapCatalog.vue'
import MapTools from './mapTools.vue'

// import { FlyTo } from './hooks/flyTo'
// import trafficApi from 'apis/traffic.js'
const { map } = initMap() // 初始化地图
const { user } = useUserStore()
const currentBaseMap = ref('vec') // 底图切换

// trafficApi.getAll().then((res) => {
// console.log(res)
// })
/*------------- 底图切换 --------------*/
function changeBaseMap(name) {
  if (name == currentBaseMap.value) return
  currentBaseMap.value = name
  switch (name) {
    case 'vec':
      map.value.getLayers().getArray()[0].setVisible(true)
      map.value.getLayers().getArray()[1].setVisible(false)
      break
    case 'img':
      map.value.getLayers().getArray()[0].setVisible(false)
      map.value.getLayers().getArray()[1].setVisible(true)
      break
  }
}
// nextTick(() => {})
</script>

<!-- 弹窗 -->
<style>
/* 2. 给popup设置样式 */
.ol-popup {
  position: absolute;
  background-color: white;
  -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  padding: 15px;
  border-radius: 10px;
  border: 1px solid #cccccc;
  bottom: 45px;
  left: -50px;
  z-index: 999;
}

.ol-popup:after,
.ol-popup:before {
  top: 100%;
  border: solid transparent;
  content: ' ';
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}

.ol-popup:after {
  border-top-color: white;
  border-width: 10px;
  left: 48px;
  margin-left: -10px;
}

.ol-popup:before {
  border-top-color: #cccccc;
  border-width: 11px;
  left: 48px;
  margin-left: -11px;
}

.ol-popup-closer {
  text-decoration: none;
  position: absolute;
  top: 2px;
  right: 8px;
}

.ol-popup-closer:after {
  content: '✖';
}

#popup-content {
  font-size: 14px;
  font-family: '微软雅黑';
}

#popup-content video {
  width: 300px;
  height: 200px;
}
</style>

<!-- 控件 -->
<style>
/* 导航控件 */
/* .ol-zoom {
  right: 0.5em;
} */
/* 缩放控件 */
.ol-zoomslider {
  top: 7.5em;
}
/* 鼠标位置控件层样式设置 */
#mouse-position {
  position: absolute;
  bottom: 0;
  /* right: 300px; */
  text-align: center;
  width: 100vw;
  /* height: 20px; */
  /*在地图容器中的层，要设置z-index的值让其显示在地图上层*/
  z-index: 2;
}
/* 鼠标位置信息自定义样式设置 */
.custom-mouse-position {
  margin: 0 auto;
  width: fit-content;
  color: rgb(0, 0, 0);
  /* background: rgba(0, 0, 0, 0.1); */
  padding: 5px 10px;
  font-size: 16px;
  font-family: '微软雅黑';
}
/*=S 自定义鹰眼样式 */
.ol-custom-overviewmap,
.ol-custom-overviewmap.ol-uncollapsible {
  border: 1px solid transparent;
  bottom: auto;
  left: auto;
  /* 右侧显示 */
  right: 5px;
  /* 顶部显示 */
  top: 5px;
} /* 鹰眼控件展开时控件外框的样式 */
.ol-custom-overviewmap:not(.ol-collapsed) {
  border: 2px solid rgba(124, 154, 190, 0.8);
}
/* 鹰眼控件中地图容器样式 */
.ol-custom-overviewmap .ol-overviewmap-map {
  border: none;
  width: 250px;
}
/* 鹰眼控件中显示当前窗口中主图区域的边框 */
.ol-custom-overviewmap .ol-overviewmap-box {
  border-radius: 5px;
  border: 2px solid #3a8fe2;
}
/* 鹰眼控件展开时其控件按钮图标的样式 */
.ol-custom-overviewmap:not(.ol-collapsed) button {
  bottom: auto;
  left: auto;
  right: 1px;
  top: 1px;
}

.ol-zoomslider {
  top: 7.5em;
  background: rgba(0, 0, 0, 0.2);
  transition: all 0.2s;
}
.ol-zoomslider:hover {
  background: rgba(0, 0, 0, 0.3);
}
/*=E 自定义鹰眼样式 */
/* 切换 */
</style>

<style scoped>
.map-container {
  position: relative;
  width: 100%;
  /* height: 100%; */
  height: calc(100% - 58px);
}
#map {
  width: 100%;
  height: 100%;
}
/*------------- 底图切换 --------------*/

.change-base-map {
  position: absolute;
  bottom: 5px;
  right: 0;
  z-index: 3;
}
.change-base-map img {
  width: 99px;
  height: 72px;
  margin: 5px;
  border-radius: 5px;
  transition: all 0.5s;
}
/* 当前 */
img.currentBaseMap {
  box-shadow: 0px 0px 3px 3px #3a8fe2;
}
</style>
